<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/3/16
 * Time: 10:37
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\Coupon as CouponService;
use app\api\validate\IDMustBePositiveInt;
use app\lib\enum\FollowStatusEnum;
use app\lib\exception\CouponException;
use app\lib\exception\SuccessMessage;
use app\api\service\Token;
use think\Hook;

class Coupon extends BaseController
{
    /**
     * 获取优惠卷列表
     * @param $wu_id
     * @return false|string
     * @throws CouponException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($wu_id){
        $t_id = Token::getCurrentTokenVar('manager_id');
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $coupon = new CouponService();
        $result = $coupon->getList($t_id,$wu_id,$uid);
        if (empty($result)){
            throw new CouponException([
                'code'=>201,
                'msg'=>'没有优惠券',
                'error_code'=>90015
            ]);
        }
        return json_encode($result);
    }

    /**
     * 获取产品购物卷列表
     * @param $product_id
     * @return false|string
     * @throws CouponException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function getListByProduct($product_id,$wu_id){
        $t_id = Token::getCurrentTokenVar('manager_id');
        $uid = Token::getCurrentUid();
        $data = explode('_',$wu_id);
        $wu_id = $data[1];
        $coupon = new CouponService();
        $result = $coupon->getListByProduct($uid,$wu_id,$t_id,$product_id);
        if (empty($result)){
            throw new CouponException([
                'code'=>201,
                'msg'=>'没有优惠券',
                'error_code'=>90015
            ]);
        }
        return json_encode($result);
    }

    /**
     * 领取优惠卷
     * @param $w_id
     * @param $c_id
     * @return false|string
     * @throws CouponException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function getCode($w_id,$c_id){
        $uid = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('manager_id');
        //行为监听
        $param = ['wu_id'=>$w_id,'user_id'=>$uid,'weight'=>FollowStatusEnum::COUPON,'data'=>''];
        Hook::exec('app\api\behavior\FollowBehavior','actionBegin',$param);
        $coupon = new CouponService();
        $result = $coupon->getCoupon($uid,$w_id,$t_id,$c_id);
        if (!$result){
            throw new CouponException([
                'code'=>201,
                'msg'=>'领取优惠卷失败',
                'error_code'=>90031
            ]);
        }
        return json_encode(new SuccessMessage());
    }

    /**
     * 获取用户优惠卷
     * @param $type
     * @return false|string
     * @throws CouponException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserCoupon($type){
        $uid = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->getCouponByUser($uid,$type);
        if (empty($result)){
            throw new CouponException([
                'code'=>201,
                'msg'=>'该用户没有此情况的优惠卷',
                'error_code'=>90040
            ]);
        }
        return json_encode($result);
    }

    /**
     * 获取优惠卷详情
     * @param $c_id
     * @return false|string
     * @throws CouponException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponDetail($c_id){
        $uid = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->getCouponDetail($c_id);
        if (empty($result)){
            throw new CouponException([
                'code'=>201,
                'msg'=>'该用户没有此情况的优惠卷',
                'error_code'=>90040
            ]);
        }
        return json_encode($result);
    }
    /**
     * 获取所有优惠卷
     * @return false|string
     * @throws CouponException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponListByTid(){
        $t_id = Token::getCurrentTokenVar('t_id');
        $coupon = new CouponService();
        $result = $coupon->getCouponListByTid($t_id);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90015;
            $data['content'] =[
                'code'=>201,
                'msg'=>'没有优惠券',
                'error_code'=>90015
            ];
        }
        return json_encode($data);
    }
    /**
     * 获取优惠卷二维码
     * @param $c_id
     * @param $page
     * @return false|string
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function getCouponQRCode($c_id,$page){
        $wu_id = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('t_id');
        $coupon = new CouponService();
        $result =  $coupon->getCouponQRCode($wu_id,$t_id,$c_id,$page);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90030;
            $data['content'] =[
                'code'=>201,
                'msg'=>'没有优惠券',
                'error_code'=>90030
            ];
        }
        return json_encode($data);
    }

    /**
     * 获客详情
     * @param $c_id
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponUser($c_id){
        $wu_id = Token::getCurrentUid();
        $coupon = new CouponService();
       $result = $coupon->getQRcodeUser($c_id,$wu_id);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90070;
            $data['content'] =[
                'msg'=>'没有获客',
                'error_code'=>90070
            ];
        }
        return json_encode($data);
    }

    /**
     * 获取用户自己的优惠卷
     * @return false|string
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserCouponList(){
        $wu_id = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('t_id');
        $coupon = new CouponService();
        $result = $coupon->getUserCoupon($t_id,$wu_id);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90080;
            $data['content'] =[
                'msg'=>'没有优惠卷',
                'error_code'=>90070
            ];
        }
        return json_encode($data);
    }

    /**
     * 添加优惠卷
     * @return false|string
     * @throws CouponException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function addCouponByUser(){
        $wu_id = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('t_id');
        $data = input("post.");
        $coupon = new CouponService();
        $result = $coupon->addCoupon($data,$t_id,$wu_id);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = [
                'msg'=>'添加成功',
                'error_code'=>200
            ];
        }else{
            $data['code'] = 90016;
            $data['content'] =[
                'msg'=>'添加优惠卷出错',
                'error_code'=>90016
            ];
        }
        return json_encode($data);
    }

    /**
     * 用户优惠卷上下架
     * @return false|string
     * @throws CouponException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function batchUpdateCouponByUser(){
        $data = input('post.');
        $ids = $data['product'];
        $type = $data['type'];
        $t_id = Token::getCurrentTokenVar('t_id');
        $coupon = new CouponService();
        $result = $coupon->batchUpdateCoupon($t_id,$ids,$type);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = 'success';
        }else{
            $data['code'] = 90017;
            $data['content'] =[
                'msg'=>'上下架优惠卷出错',
                'error_code'=>90017
            ];
        }
        return json_encode($data);
    }

    /**
     * 获取优惠卷详情
     * @param $c_id
     * @return false|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponDetailByUser($c_id){
        $uid = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->getCouponDetail($c_id);
        if (empty($result)){
            $data['code'] = 90018;
            $data['content'] =[
                'msg'=>'该用户没有此情况的优惠卷',
                'error_code'=>90040
            ];
        }else{
            $data['code'] = 200;
            $data['content'] = $result;
        }
        return json_encode($data);
    }
    /**----------------- 优惠券管理方法 -----------------**/
    /**
     * 获取优惠卷列表
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws CouponException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponList($type){
        $t_id = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->getCouponList($t_id,$type);
        if ($result->isEmpty()){
            throw new CouponException([
                'code'=>'201',
                'msg'=>'你还未有名片列表',
                'error_code'=>90001
            ]);
        }
        return $result;
    }

    /**
     * 添加购物券
     * @return \think\response\Json
     * @throws CouponException
     */
    public function addCoupon(){
        $t_id = Token::getCurrentUid();
        $data = input("post.");
        $coupon = new CouponService();
        $result = $coupon->addCoupon($data,$t_id);
        if (!$result){
            throw new CouponException([
               'msg'=>'添加购物券出错'
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 获取购物卷详情
     * @param $id
     * @return false|string
     * @throws CouponException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponById($id){
       $t_id = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->getCouponById($id);
        if (!$result){
            throw new CouponException([
                'msg'=>'获取购物卷出错'
            ]);
        }
        return json_encode($result);
    }

    /**
 * 更新购物券
 * @return \think\response\Json
 * @throws CouponException
 */
    public function updateCoupon(){
        $t_id = Token::getCurrentUid();
        $data = input("post.");
        $coupon = new CouponService();
        $result = $coupon->updateCoupon($data,$t_id);
        if (!$result){
            throw new CouponException([
                'msg'=>'更新购物券出错'
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 批量上下架
     * @return \think\response\Json
     * @throws CouponException
     */
    public function batchUpdateCoupon(){
        $data = input('post.');
        $ids = $data['product'];
        $type = $data['type'];
        $t_id = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->batchUpdateCoupon($t_id,$ids,$type);
        if (!$result){
            throw new CouponException([
                'msg'=>'更新购物券出错'
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 更新购物券
     * @return \think\response\Json
     * @throws CouponException
     */
    public function deleteCoupon($id){
        (new IDMustBePositiveInt())->goCheck();
        $t_id = Token::getCurrentUid();
        $coupon = new CouponService();
        $result = $coupon->deleteCoupon($id);
        if (!$result){
            throw new CouponException([
                'msg'=>'更新购物券出错'
            ]);
        }
        return json(new SuccessMessage());
    }

}